home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / chapter13 / DoubleBuff.java < prev    next >
Text File  |  1995-12-31  |  2KB  |  68 lines

  1.  
  2. import java.awt.*;
  3.  
  4. public class DoubleBuff extends SingleBuff implements Runnable {
  5.  
  6.    /* double buffering stuff */
  7.    Graphics dbuffer; // Graphics object for offscreen drawing
  8.    Image offscreen;  // Image representing the final screen 
  9.               //  picture
  10.  
  11.    public void init() {
  12.     AppBorder = bounds(); // get current applet boundary
  13.     XRES = AppBorder.width;
  14.     YRES = AppBorder.height;
  15.     x=XRES/2-w/2; //center the image
  16.  
  17.     /* Create an image that is the same size */
  18.     /* as the resolution */
  19.     offscreen = createImage(XRES,YRES); 
  20.  
  21.     dbuffer = offscreen.getGraphics();  //associate a Graphics obj    
  22.     dbuffer.setColor(Color.lightGray);  //background color
  23.     dbuffer.fillRect(0,0,XRES,YRES); //fill in background
  24.    }
  25.  
  26.  
  27.    /* The paint method */
  28.    public void paint(Graphics g) {
  29.  
  30.       dbuffer.setColor(Color.lightGray); // background color
  31.       dbuffer.fillRect(x,oldy,w+1,h+1);  // cover up last box drawn
  32.       oldy=y;               // remember y for the next coverup
  33.  
  34.       /* draw background pattern */
  35.       for(int gc=0;gc<XRES;gc+=gw) { 
  36.         dbuffer.setColor(Color.gray);        // color of gridlines
  37.         dbuffer.drawLine(gc,0,gc,YRES);      // draw vertical lines
  38.         dbuffer.drawLine(0,gc,XRES,gc);      // draw horizontal lines
  39.         dbuffer.setColor(Color.yellow);      // set color to yellow
  40.         dbuffer.drawLine(gc,0,0,gc);         // draw diags
  41.         dbuffer.setColor(Color.blue);        // set color to blue
  42.         dbuffer.drawLine(XRES-gc,0,XRES,gc); // draw diags
  43.         }
  44.  
  45.       dbuffer.setColor(Color.lightGray); // background color
  46.       dbuffer.fillRect(0,0,100,20);      // clear box for title
  47.       dbuffer.setColor(Color.red);       // set color to red
  48.       dbuffer.drawString(XRES + " " + YRES,0,10);
  49.       dbuffer.fillRect(x,y,w,h);          // draw red square
  50.       dbuffer.setColor(Color.blue); 
  51.  
  52.       int l=0;
  53.       for (int lh=h;lh>0;l+=4,lh-=8)   
  54.           dbuffer.drawOval(x+l,y+l,lh,lh);
  55.  
  56.     /* Notice that the only call to the g object    */
  57.     /* is on the following line. All other graphics */
  58.     /* operations happen to the buffer         */
  59.  
  60.     /* Transfer (blit) the offscreen to the screen  */
  61.       g.drawImage(offscreen,0,0,this);
  62.       }
  63.  
  64.     
  65. }
  66.  
  67.  
  68.